package com.platform.user.center.portal.task;

import com.platform.user.center.portal.entity.VirtualTransitCard;
import com.platform.user.center.portal.service.VirtualTransitCardService;
import com.platform.user.center.portal.util.DateUtils;
import com.platform.user.center.portal.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by churcy on 2017/6/14.
 */
@Component
public class RevokeCardTask {

    @Autowired
    private VirtualTransitCardService virtualTransitCardService;

    @Scheduled(cron = "23 23 23 * *  ?")
    public void syncAlipayRevokeCard() {
        try {
            String url = "http://101.37.170.13:8080/api/virtual/card/local/revoked";
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("cardStatus", "disabled");
            conditions.put("checkDate", DateUtils.format2String(new Date(), "yyyy-MM-dd"));
            List<Map<String, String>> virtualTransitCards = virtualTransitCardService.findByConditions(conditions);

            for (Map<String, String> card : virtualTransitCards) {
                StringBuffer sb = new StringBuffer();
                sb.append("userId=" + card.get("userId"));
                sb.append("&action=" + "revoke");
                sb.append("&disabledTips=" + "系统统一退卡");
                String apiResult = HttpUtils.httpPost(url, null, sb.toString());
            }
            System.out.println("退卡完成");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
